<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * All class for user
 *
 * @author Shark
 */
class Az_User {

    private $nametbl = 'az_users';
    //All atribute of user object
    var $userId;
    var $userName;
    var $password;
    var $name;
    var $birthday;
    var $phone;
    var $email;
    var $address;
    var $status;
    var $sex;
    var $date_create;
    var $active;
    var $role_id;


    /*
     * Constructor function
     */

    public function createUser($_userId, $_userName, $_password, $_name, $_birthday, $_phone, $_email, $_address, $_status, $_sex, $_date_create, $_active, $_roleId) {
        if ($_userId != null && is_numeric($_userId)) {
            $this->userId = $_userId;
        } else {
            $this->userId = null;
        }
        $this->userName = $_userName;
        $this->password = $_password;
        $this->name = $_name;
        $this->birthday = $_birthday;
        $this->phone = $_phone;
        $this->email = $_email;
        $this->address = $_address;
        $this->status = $_status;
        $this->sex = $_sex;
        if ($_date_create != null) {
            $this->date_create = $_date_create;
        } else {
            $this->date_create = date("d/m/Y");
        }
        $this->active = $_active;
        $this->role_id = $_roleId;
    }

    /*
     * Function add new user
     */

    function addNewUser() {
        global $az_db;
        if ($this->userName != null && $this->password != null) {
            $sql = "INSERT INTO " . $this->nametbl .
                    "(`username`, `password`, `name`, `birthday`, `phone`, 
                     `email`, `address`, `status`, `sex`, `date_create`) 
                     VALUES(?,?,?,?,?,?,?,?,?,?)";
            $params = array($this->userName, $this->encode_data($this->password),
                $this->name, $this->birthday, $this->phone, $this->email, $this->address,
                $this->status, $this->sex, $this->date_create);
            return $az_db->executeStmt_Az($sql, 'sssssssiis', $params);
        }
    }

    /*
     * Function check login user
     */

    function check_login_user($username, $password) {
        global $az_db;
        $_pass = $this->encode_data($password);
        $sql = "SELECT username,password FROM az_users WHERE username='" . $username . "' and password='" . $_pass . "'";
        $result = $az_db->executeSQL_Query($sql);
        $stt = false;
        if ($result->num_rows > 0) {
            while ($rows = $result->fetch_array(MYSQLI_ASSOC)) {
                if ($rows['username'] == $username && $rows['password'] == $this->encode_data($password)) {
                    $stt = true;                 
                }
            }
            $result->free();
            return stt;
        }
        $result->free();
        return stt;
    }

    /*
     * Function update info of a user
     */

    function updateUser() {
        global $az_db;
        if (is_numeric($this->userId) && $this->userId != null) {
            $type_format = '';
            $params = array();
            $sql = "UPDATE " . $this->nametbl . " SET ";
            if ($this->userName != null) {
                $sql.=" username  = ? ,";
                $type_format.='s';
                array_push($params, $this->userName);
            }
            if ($this->password != null) {
                $sql.=" password = ? ,";
                $type_format.='s';
                array_push($params, $this->password);
            }
            if ($this->name != null) {
                $sql.=" name =? ,";
                $type_format.='s';
                array_push($params, $this->name);
            }
            if ($this->birthday != null) {
                $sql.=" birthday =? ,";
                $type_format.='s';
                array_push($params, $this->birthday);
            }
            if ($this->phone != null) {
                $sql.=" phone=? ,";
                $type_format.='s';
                array_push($params, $this->phone);
            }
            if ($this->email != null) {
                $sql.=" email=? ,";
                $type_format.='s';
                array_push($params, $this->email);
            }
            if ($this->address != null) {
                $sql.=" address=? ,";
                $type_format.='s';
                array_push($params, $this->address);
            }
            if ($this->sex != null) {
                $sql.=" sex =? ";
                $type_format.='i';
                array_push($params, $this->sex);
            }
            $sql.= " WHERE user_id = ?";
            $type_format.='i';
            array_push($params, $this->userId);
            return $az_db->executeStmt_Az($sql, $type_format, $params);
        }
    }

    /*
     * Function encode data
     */

    private function encode_data($data) {
        return md5(utf8_decode($data));
    }

    //Delete user
    function deleteUser($userId) {
        if ($userId != null && is_numeric($userId)) {
            global $az_db;
            $sql = "DELETE FROM az_users WHERE user_id = " . $userId;
            return $az_db->executeSQL_Query($sql);
        }
    }

    /*
     * Function get once user by user id
     */

    function getOnceUser($userId) {
        $sql = "SELECT * FROM az_users WHERE user_id = " . $userId;
        $result = mysqli_query($conn, $sql) or die(mysqli_error($conn));
        if (mysqli_num_rows($result) >= 1) {
            $tmp = new Az_user();
            while ($rows = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
                $tmp->userId = $rows['user_id'];
                $tmp->userName = $rows['username'];
                $tmp->password = $rows['password'];
                $tmp->name = $rows['name'];
                $tmp->birthday = $rows['birthday'];
                $tmp->phone = $rows['phone'];
                $tmp->email = $rows['email'];
                $tmp->address = $rows['address'];
                $tmp->status = $rows['status'];
                $tmp->sex = $rows['sex'];
                $tmp->date_create = $rows['date_create'];
                $tmp->active = $rows['active'];
                $tmp->role_id = $rows['role_id'];
            }
            return $tmp;
        } else {
            return null;
        }
    }

    /*
     * Function get all user 
     * @return Az_user object is array
     */

    function getAllUser() {
        $sql = "SELECT * FROM az_users";
        $result = mysqli_query($conn, $sql) or die(mysqli_error($conn));
        if (mysqli_num_rows($result) >= 1) {
            $tmp = array();
            while ($rows = mysqli_fetch_row($result)) {
                $tmpUser = new Az_user();
            }
        }
    }

    /*
     * 
     */

    function checkLogonSystem($userName, $userpass) {
        
    }

}

?>
